package kn.cstf2.main.Client;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.Map;
import kn.cstf2.assign.Bean.ListaTablaBean;
import kn.cstf2.assign.Server.AssignServer;
import kn.cstf2.global.Utils.CstF2Exception;
import kn.cstf2.login.Client.LoginAction;
import kn.cstf2.main.Server.MainServer;
import kn.cstf2.main.Shared.MainShared;
import kn.cstf2.transition.Bean.TransitionBean;
import kn.mx.cstf2.dto.UsuarioDto;

import org.apache.log4j.Logger;

/**
 * @author user1
 */
public class MainAction extends ActionSupport {

    private static Logger log = Logger.getLogger(LoginAction.class);
    public static final String ERROR_SESSION = "errorSession";
    private TransitionBean transitionBean = new TransitionBean();
    private ArrayList<TransitionBean> transitionBeanArray;
    private ArrayList<ListaTablaBean> listaTablaBeans;
    private String knkey;
    private Integer knkey2;

    public String getKnkey() {
        return knkey;
    }

    public void setKnkey(String knkey) {
        this.knkey = knkey;
    }

    public ArrayList<TransitionBean> getTransitionBeanArray() {
        return transitionBeanArray;
    }

    public void setTransitionBeanArray(ArrayList<TransitionBean> transitionBeanArray) {
        this.transitionBeanArray = transitionBeanArray;
    }

    public ArrayList<ListaTablaBean> getListaTablaBeans() {
        return listaTablaBeans;
    }

    public void setListaTablaBeans(ArrayList<ListaTablaBean> listaTablaBeans) {
        this.listaTablaBeans = listaTablaBeans;
    }

    public Integer getKnkey2() {
        return knkey2;
    }

    public void setKnkey2(Integer knkey2) {
        this.knkey2 = knkey2;
    }
    
    public MainAction() {
        UsuarioDto usuario = new UsuarioDto();
        ActionContext contexto = ActionContext.getContext();
        AssignServer assignServer = new AssignServer();
        Map<String, Object> sesion = contexto.getSession();
        usuario = (UsuarioDto) sesion.get("usuarioSession");

        MainShared mainShared = new MainShared();
        setTransitionBeanArray(mainShared.obtenerKnkeysandClientNames(usuario));
        listaTablaBeans = assignServer.obtenerListaTabla(usuario.getId());
    }

    @Override
    public String execute() {
        try {
            if (transitionBean != null) {
                UsuarioDto usuario = new UsuarioDto();
                ActionContext contexto = ActionContext.getContext();
                Map<String, Object> sesion = contexto.getSession();
                usuario = (UsuarioDto) sesion.get("usuarioSession");
                cargarKnkeyOnSession();
            } else {
                log.info("No hay Knkeys asignadas.");
                addFieldError("KnkeyIncorrecta", "No hay Knkeys asignadas.");
                return ERROR_SESSION;
            }
        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        return SUCCESS;
    }

    public void cargarKnkeyOnSession() {
        TransitionBean transitionBean = new TransitionBean();
        if (knkey2 != null) {
            MainServer ms = new MainServer();
            transitionBean.setKnkey(knkey2);

            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesion2 = contexto.getSession();
            sesion2.put("Knkey", transitionBean);
            ms.updatedFalse(knkey2);
        }
    }
}
